package weather.gui.panels.utils;

import java.awt.Color;

import javax.swing.text.AttributeSet;
import javax.swing.JTextPane;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

@SuppressWarnings("serial")
/**
 * The extension to the JTextPane from swing components
 * This extension enable to simply append text to panel with specified
 * style properties
 */
public class ExtendedTextPanel extends JTextPane
{
	/**
	 * 
	 * Append text to the panel with specified style properties
	 * 
	 * @param c				text color
	 * @param s				appended text
	 * @param size			size of appended text
	 * @param background	background text color
	 */
	  public void append(Color c, String s, String fontFamily, boolean italic, int size, Color background) 
	  { 
		  setEditable(true);
		  StyleContext sc = StyleContext.getDefaultStyleContext();
		  AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
				  		StyleConstants.Foreground, c);
		  aset = sc.addAttribute(aset,
			  		StyleConstants.FontSize, size);
		  aset = sc.addAttribute(aset,
			  		StyleConstants.Background, background);
		  aset = sc.addAttribute(aset,
				  StyleConstants.FontFamily, fontFamily);
		  aset = sc.addAttribute(aset,
				  StyleConstants.Italic, italic);
		  int len = getDocument().getLength(); // same value as
	                       // getText().length();
		  setCaretPosition(len); // place caret at the end (with no selection)
		  setCharacterAttributes(aset, false);
		  replaceSelection(s); // there is no selection, so inserts at caret
		  setEditable(false);
	  }
}
